package com.android.billingclient.api;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.ResultReceiver;
import android.text.TextUtils;
import c.b.a.a.a.a.c;
import com.android.billingclient.api.g;
import com.android.billingclient.api.j;
import com.android.billingclient.api.l;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.json.JSONException;

class d extends c {
    /* access modifiers changed from: private */

    /* renamed from: a  reason: collision with root package name */
    public int f2037a;
    /* access modifiers changed from: private */

    /* renamed from: b  reason: collision with root package name */
    public final String f2038b;

    /* renamed from: c  reason: collision with root package name */
    private final Handler f2039c;
    /* access modifiers changed from: private */

    /* renamed from: d  reason: collision with root package name */
    public g0 f2040d;
    /* access modifiers changed from: private */
    public Context e;
    private final int f;
    private final int g;
    /* access modifiers changed from: private */
    public c.b.a.a.a.a.a h;
    private a i;
    /* access modifiers changed from: private */
    public boolean j;
    /* access modifiers changed from: private */
    public boolean k;
    /* access modifiers changed from: private */
    public boolean l;
    /* access modifiers changed from: private */
    public boolean m;
    /* access modifiers changed from: private */
    public boolean n;
    /* access modifiers changed from: private */
    public boolean o;
    /* access modifiers changed from: private */
    public boolean p;
    /* access modifiers changed from: private */
    public boolean q;
    private boolean r;
    private ExecutorService s;
    private String t;
    private final ResultReceiver u;

    private final class a implements ServiceConnection {
        /* access modifiers changed from: private */

        /* renamed from: a  reason: collision with root package name */
        public final Object f2041a;
        /* access modifiers changed from: private */

        /* renamed from: b  reason: collision with root package name */
        public boolean f2042b;
        /* access modifiers changed from: private */

        /* renamed from: c  reason: collision with root package name */
        public e f2043c;

        private a(e eVar) {
            this.f2041a = new Object();
            this.f2042b = false;
            this.f2043c = eVar;
        }

        /* synthetic */ a(d dVar, e eVar, k0 k0Var) {
            this(eVar);
        }

        /* access modifiers changed from: private */
        public final void d(g gVar) {
            d.this.w(new w(this, gVar));
        }

        /* access modifiers changed from: package-private */
        public final void b() {
            synchronized (this.f2041a) {
                this.f2043c = null;
                this.f2042b = true;
            }
        }

        public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            c.a.a.a.a.j("BillingClient", "Billing service connected.");
            c.b.a.a.a.a.a unused = d.this.h = c.x0(iBinder);
            if (d.this.s(new y(this), 30000, new x(this)) == null) {
                d(d.this.A());
            }
        }

        public final void onServiceDisconnected(ComponentName componentName) {
            c.a.a.a.a.k("BillingClient", "Billing service disconnected.");
            c.b.a.a.a.a.a unused = d.this.h = null;
            int unused2 = d.this.f2037a = 0;
            synchronized (this.f2041a) {
                if (this.f2043c != null) {
                    this.f2043c.a();
                }
            }
        }
    }

    private d(Context context, int i2, int i3, boolean z, k kVar, String str, String str2) {
        this.f2037a = 0;
        this.f2039c = new Handler(Looper.getMainLooper());
        this.u = new k0(this, this.f2039c);
        this.t = str2;
        this.f = i2;
        this.g = i3;
        this.f2038b = str;
        j(context, kVar, z);
    }

    d(String str, boolean z, int i2, Context context, k kVar, int i3) {
        this(context, i2, i3, z, kVar, q(), str);
    }

    /* access modifiers changed from: private */
    public final g A() {
        int i2 = this.f2037a;
        return (i2 == 0 || i2 == 3) ? b0.n : b0.j;
    }

    /* access modifiers changed from: private */
    public final j.a B(String str) {
        String valueOf = String.valueOf(str);
        c.a.a.a.a.j("BillingClient", valueOf.length() != 0 ? "Querying owned items, item type: ".concat(valueOf) : new String("Querying owned items, item type: "));
        ArrayList arrayList = new ArrayList();
        Bundle d2 = c.a.a.a.a.d(this.n, this.r, this.f2038b);
        String str2 = null;
        do {
            try {
                Bundle H = this.n ? this.h.H(9, this.e.getPackageName(), str, str2, d2) : this.h.Z(3, this.e.getPackageName(), str, str2);
                g a2 = d0.a(H, "BillingClient", "getPurchase()");
                if (a2 != b0.m) {
                    return new j.a(a2, (List<j>) null);
                }
                ArrayList<String> stringArrayList = H.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
                ArrayList<String> stringArrayList2 = H.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
                ArrayList<String> stringArrayList3 = H.getStringArrayList("INAPP_DATA_SIGNATURE_LIST");
                int i2 = 0;
                while (i2 < stringArrayList2.size()) {
                    String str3 = stringArrayList2.get(i2);
                    String str4 = stringArrayList3.get(i2);
                    String valueOf2 = String.valueOf(stringArrayList.get(i2));
                    c.a.a.a.a.j("BillingClient", valueOf2.length() != 0 ? "Sku is owned: ".concat(valueOf2) : new String("Sku is owned: "));
                    try {
                        j jVar = new j(str3, str4);
                        if (TextUtils.isEmpty(jVar.c())) {
                            c.a.a.a.a.k("BillingClient", "BUG: empty/null token!");
                        }
                        arrayList.add(jVar);
                        i2++;
                    } catch (JSONException e2) {
                        String valueOf3 = String.valueOf(e2);
                        StringBuilder sb = new StringBuilder(String.valueOf(valueOf3).length() + 48);
                        sb.append("Got an exception trying to decode the purchase: ");
                        sb.append(valueOf3);
                        c.a.a.a.a.k("BillingClient", sb.toString());
                        return new j.a(b0.j, (List<j>) null);
                    }
                }
                str2 = H.getString("INAPP_CONTINUATION_TOKEN");
                String valueOf4 = String.valueOf(str2);
                c.a.a.a.a.j("BillingClient", valueOf4.length() != 0 ? "Continuation token: ".concat(valueOf4) : new String("Continuation token: "));
            } catch (Exception e3) {
                String valueOf5 = String.valueOf(e3);
                StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf5).length() + 57);
                sb2.append("Got exception trying to get purchases: ");
                sb2.append(valueOf5);
                sb2.append("; try to reconnect");
                c.a.a.a.a.k("BillingClient", sb2.toString());
                return new j.a(b0.n, (List<j>) null);
            }
        } while (!TextUtils.isEmpty(str2));
        return new j.a(b0.m, arrayList);
    }

    private void j(Context context, k kVar, boolean z) {
        Context applicationContext = context.getApplicationContext();
        this.e = applicationContext;
        this.f2040d = new g0(applicationContext, kVar);
        this.r = z;
    }

    private final g l(g gVar) {
        this.f2040d.c().a(gVar, (List<j>) null);
        return gVar;
    }

    private static String q() {
        try {
            return (String) Class.forName("com.android.billingclient.ktx.BuildConfig").getField("VERSION_NAME").get((Object) null);
        } catch (Exception unused) {
            return "2.2.0";
        }
    }

    /* access modifiers changed from: private */
    public final <T> Future<T> s(Callable<T> callable, long j2, Runnable runnable) {
        long j3 = (long) (((double) j2) * 0.95d);
        if (this.s == null) {
            this.s = Executors.newFixedThreadPool(c.a.a.a.a.f1811a);
        }
        try {
            Future<T> submit = this.s.submit(callable);
            this.f2039c.postDelayed(new t0(this, submit, runnable), j3);
            return submit;
        } catch (Exception e2) {
            String valueOf = String.valueOf(e2);
            StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 28);
            sb.append("Async task throws exception ");
            sb.append(valueOf);
            c.a.a.a.a.k("BillingClient", sb.toString());
            return null;
        }
    }

    /* access modifiers changed from: private */
    public final void v(h hVar, i iVar) {
        String str;
        int i2;
        String b2 = hVar.b();
        try {
            String valueOf = String.valueOf(b2);
            c.a.a.a.a.j("BillingClient", valueOf.length() != 0 ? "Consuming purchase with token: ".concat(valueOf) : new String("Consuming purchase with token: "));
            if (this.n) {
                Bundle O = this.h.O(9, this.e.getPackageName(), b2, c.a.a.a.a.b(hVar, this.n, this.f2038b));
                int i3 = O.getInt("RESPONSE_CODE");
                str = c.a.a.a.a.g(O, "BillingClient");
                i2 = i3;
            } else {
                i2 = this.h.i0(3, this.e.getPackageName(), b2);
                str = "";
            }
            g.a b3 = g.b();
            b3.c(i2);
            b3.b(str);
            g a2 = b3.a();
            if (i2 == 0) {
                w(new q(this, iVar, a2, b2));
            } else {
                w(new p(this, i2, iVar, a2, b2));
            }
        } catch (Exception e2) {
            w(new r(this, e2, iVar, b2));
        }
    }

    /* access modifiers changed from: private */
    public final void w(Runnable runnable) {
        if (!Thread.interrupted()) {
            this.f2039c.post(runnable);
        }
    }

    public void a(a aVar, b bVar) {
        g gVar;
        if (!d()) {
            gVar = b0.n;
        } else if (TextUtils.isEmpty(aVar.b())) {
            c.a.a.a.a.k("BillingClient", "Please provide a valid purchase token.");
            gVar = b0.i;
        } else if (!this.n) {
            gVar = b0.f2030b;
        } else if (s(new p0(this, aVar, bVar), 30000, new s0(this, bVar)) == null) {
            bVar.a(A());
            return;
        } else {
            return;
        }
        bVar.a(gVar);
    }

    public void b(h hVar, i iVar) {
        if (!d()) {
            iVar.a(b0.n, (String) null);
        } else if (s(new o0(this, hVar, iVar), 30000, new n0(this, iVar)) == null) {
            iVar.a(A(), (String) null);
        }
    }

    public void c() {
        try {
            this.f2040d.d();
            if (this.i != null) {
                this.i.b();
            }
            if (!(this.i == null || this.h == null)) {
                c.a.a.a.a.j("BillingClient", "Unbinding from service.");
                this.e.unbindService(this.i);
                this.i = null;
            }
            this.h = null;
            if (this.s != null) {
                this.s.shutdownNow();
                this.s = null;
            }
        } catch (Exception e2) {
            String valueOf = String.valueOf(e2);
            StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 48);
            sb.append("There was an exception while ending connection: ");
            sb.append(valueOf);
            c.a.a.a.a.k("BillingClient", sb.toString());
        } finally {
            this.f2037a = 3;
        }
    }

    public boolean d() {
        return (this.f2037a != 2 || this.h == null || this.i == null) ? false : true;
    }

    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r15v9, resolved type: com.android.billingclient.api.t} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r15v11, resolved type: com.android.billingclient.api.v} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r15v15, resolved type: com.android.billingclient.api.t} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r2v14, resolved type: com.android.billingclient.api.t} */
    /* JADX WARNING: Multi-variable type inference failed */
    /* JADX WARNING: Removed duplicated region for block: B:69:0x0158 A[Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }] */
    /* JADX WARNING: Removed duplicated region for block: B:71:0x0180 A[Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public com.android.billingclient.api.g e(android.app.Activity r14, com.android.billingclient.api.f r15) {
        /*
            r13 = this;
            java.lang.String r0 = "BUY_INTENT"
            java.lang.String r1 = "; try to reconnect"
            boolean r2 = r13.d()
            if (r2 != 0) goto L_0x0010
        L_0x000a:
            com.android.billingclient.api.g r14 = com.android.billingclient.api.b0.n
        L_0x000c:
            r13.l(r14)
            return r14
        L_0x0010:
            java.lang.String r6 = r15.h()
            java.lang.String r8 = r15.f()
            com.android.billingclient.api.l r2 = r15.g()
            r3 = 1
            r4 = 0
            if (r2 == 0) goto L_0x0028
            boolean r5 = r2.d()
            if (r5 == 0) goto L_0x0028
            r5 = 1
            goto L_0x0029
        L_0x0028:
            r5 = 0
        L_0x0029:
            java.lang.String r9 = "BillingClient"
            if (r8 != 0) goto L_0x0035
            java.lang.String r14 = "Please fix the input params. SKU can't be null."
            c.a.a.a.a.k(r9, r14)
            com.android.billingclient.api.g r14 = com.android.billingclient.api.b0.k
            goto L_0x000c
        L_0x0035:
            if (r6 != 0) goto L_0x003f
            java.lang.String r14 = "Please fix the input params. SkuType can't be null."
            c.a.a.a.a.k(r9, r14)
            com.android.billingclient.api.g r14 = com.android.billingclient.api.b0.l
            goto L_0x000c
        L_0x003f:
            java.lang.String r7 = "subs"
            boolean r7 = r6.equals(r7)
            if (r7 == 0) goto L_0x0053
            boolean r7 = r13.j
            if (r7 != 0) goto L_0x0053
            java.lang.String r14 = "Current client doesn't support subscriptions."
            c.a.a.a.a.k(r9, r14)
            com.android.billingclient.api.g r14 = com.android.billingclient.api.b0.p
            goto L_0x000c
        L_0x0053:
            java.lang.String r7 = r15.c()
            if (r7 == 0) goto L_0x005a
            goto L_0x005b
        L_0x005a:
            r3 = 0
        L_0x005b:
            if (r3 == 0) goto L_0x0069
            boolean r4 = r13.k
            if (r4 != 0) goto L_0x0069
            java.lang.String r14 = "Current client doesn't support subscriptions update."
            c.a.a.a.a.k(r9, r14)
            com.android.billingclient.api.g r14 = com.android.billingclient.api.b0.q
            goto L_0x000c
        L_0x0069:
            boolean r4 = r15.n()
            java.lang.String r7 = "Current client doesn't support extra params for buy intent."
            if (r4 == 0) goto L_0x007b
            boolean r4 = r13.l
            if (r4 != 0) goto L_0x007b
        L_0x0075:
            c.a.a.a.a.k(r9, r7)
            com.android.billingclient.api.g r14 = com.android.billingclient.api.b0.h
            goto L_0x000c
        L_0x007b:
            if (r5 == 0) goto L_0x0082
            boolean r4 = r13.l
            if (r4 != 0) goto L_0x0082
            goto L_0x0075
        L_0x0082:
            java.lang.String r4 = java.lang.String.valueOf(r8)
            int r4 = r4.length()
            int r4 = r4 + 41
            java.lang.String r7 = java.lang.String.valueOf(r6)
            int r7 = r7.length()
            int r4 = r4 + r7
            java.lang.StringBuilder r7 = new java.lang.StringBuilder
            r7.<init>(r4)
            java.lang.String r4 = "Constructing buy intent for "
            r7.append(r4)
            r7.append(r8)
            java.lang.String r4 = ", item type: "
            r7.append(r4)
            r7.append(r6)
            java.lang.String r4 = r7.toString()
            c.a.a.a.a.j(r9, r4)
            boolean r4 = r13.l
            r10 = 0
            r11 = 5000(0x1388, double:2.4703E-320)
            if (r4 == 0) goto L_0x0131
            boolean r3 = r13.n
            boolean r4 = r13.r
            java.lang.String r7 = r13.f2038b
            android.os.Bundle r7 = c.a.a.a.a.c(r15, r3, r4, r7)
            java.lang.String r3 = r2.f()
            boolean r3 = r3.isEmpty()
            if (r3 != 0) goto L_0x00d5
            java.lang.String r3 = r2.f()
            java.lang.String r4 = "skuDetailsToken"
            r7.putString(r4, r3)
        L_0x00d5:
            java.lang.String r3 = r2.e()
            boolean r3 = android.text.TextUtils.isEmpty(r3)
            if (r3 != 0) goto L_0x00e8
            java.lang.String r3 = r2.e()
            java.lang.String r4 = "skuPackageName"
            r7.putString(r4, r3)
        L_0x00e8:
            if (r5 == 0) goto L_0x0105
            java.lang.String r2 = r2.g()
            java.lang.String r3 = "rewardToken"
            r7.putString(r3, r2)
            int r2 = r13.f
            if (r2 == 0) goto L_0x00fc
            java.lang.String r3 = "childDirected"
            r7.putInt(r3, r2)
        L_0x00fc:
            int r2 = r13.g
            if (r2 == 0) goto L_0x0105
            java.lang.String r3 = "underAgeOfConsent"
            r7.putInt(r3, r2)
        L_0x0105:
            java.lang.String r2 = r13.t
            boolean r2 = android.text.TextUtils.isEmpty(r2)
            if (r2 != 0) goto L_0x0114
            java.lang.String r2 = r13.t
            java.lang.String r3 = "accountName"
            r7.putString(r3, r2)
        L_0x0114:
            r2 = 6
            boolean r3 = r13.n
            if (r3 == 0) goto L_0x011e
            r15 = 9
            r4 = 9
            goto L_0x0128
        L_0x011e:
            boolean r15 = r15.i()
            if (r15 == 0) goto L_0x0127
            r15 = 7
            r4 = 7
            goto L_0x0128
        L_0x0127:
            r4 = 6
        L_0x0128:
            com.android.billingclient.api.t r15 = new com.android.billingclient.api.t
            r2 = r15
            r3 = r13
            r5 = r8
            r2.<init>(r3, r4, r5, r6, r7)
            goto L_0x0142
        L_0x0131:
            if (r3 == 0) goto L_0x013d
            com.android.billingclient.api.s r2 = new com.android.billingclient.api.s
            r2.<init>(r13, r15, r8)
            java.util.concurrent.Future r15 = r13.s(r2, r11, r10)
            goto L_0x0146
        L_0x013d:
            com.android.billingclient.api.v r15 = new com.android.billingclient.api.v
            r15.<init>(r13, r8, r6)
        L_0x0142:
            java.util.concurrent.Future r15 = r13.s(r15, r11, r10)
        L_0x0146:
            java.util.concurrent.TimeUnit r2 = java.util.concurrent.TimeUnit.MILLISECONDS     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            java.lang.Object r15 = r15.get(r11, r2)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            android.os.Bundle r15 = (android.os.Bundle) r15     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            int r2 = c.a.a.a.a.h(r15, r9)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            java.lang.String r3 = c.a.a.a.a.g(r15, r9)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            if (r2 == 0) goto L_0x0180
            r14 = 52
            java.lang.StringBuilder r15 = new java.lang.StringBuilder     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            r15.<init>(r14)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            java.lang.String r14 = "Unable to buy item, Error response code: "
            r15.append(r14)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            r15.append(r2)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            java.lang.String r14 = r15.toString()     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            c.a.a.a.a.k(r9, r14)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            com.android.billingclient.api.g$a r14 = com.android.billingclient.api.g.b()     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            r14.c(r2)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            r14.b(r3)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            com.android.billingclient.api.g r14 = r14.a()     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            r13.l(r14)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            return r14
        L_0x0180:
            android.content.Intent r2 = new android.content.Intent     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            java.lang.Class<com.android.billingclient.api.ProxyBillingActivity> r3 = com.android.billingclient.api.ProxyBillingActivity.class
            r2.<init>(r14, r3)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            java.lang.String r3 = "result_receiver"
            android.os.ResultReceiver r4 = r13.u     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            r2.putExtra(r3, r4)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            android.os.Parcelable r15 = r15.getParcelable(r0)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            android.app.PendingIntent r15 = (android.app.PendingIntent) r15     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            r2.putExtra(r0, r15)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            r14.startActivity(r2)     // Catch:{ CancellationException | TimeoutException -> 0x01c0, Exception -> 0x019d }
            com.android.billingclient.api.g r14 = com.android.billingclient.api.b0.m
            return r14
        L_0x019d:
            java.lang.String r14 = java.lang.String.valueOf(r8)
            int r14 = r14.length()
            int r14 = r14 + 69
            java.lang.StringBuilder r15 = new java.lang.StringBuilder
            r15.<init>(r14)
            java.lang.String r14 = "Exception while launching billing flow: ; for sku: "
            r15.append(r14)
            r15.append(r8)
            r15.append(r1)
            java.lang.String r14 = r15.toString()
            c.a.a.a.a.k(r9, r14)
            goto L_0x000a
        L_0x01c0:
            java.lang.String r14 = java.lang.String.valueOf(r8)
            int r14 = r14.length()
            int r14 = r14 + 68
            java.lang.StringBuilder r15 = new java.lang.StringBuilder
            r15.<init>(r14)
            java.lang.String r14 = "Time out while launching billing flow: ; for sku: "
            r15.append(r14)
            r15.append(r8)
            r15.append(r1)
            java.lang.String r14 = r15.toString()
            c.a.a.a.a.k(r9, r14)
            com.android.billingclient.api.g r14 = com.android.billingclient.api.b0.o
            goto L_0x000c
        */
        throw new UnsupportedOperationException("Method not decompiled: com.android.billingclient.api.d.e(android.app.Activity, com.android.billingclient.api.f):com.android.billingclient.api.g");
    }

    public j.a g(String str) {
        if (!d()) {
            return new j.a(b0.n, (List<j>) null);
        }
        if (TextUtils.isEmpty(str)) {
            c.a.a.a.a.k("BillingClient", "Please provide a valid SKU type.");
            return new j.a(b0.g, (List<j>) null);
        }
        try {
            return (j.a) s(new u(this, str), 5000, (Runnable) null).get(5000, TimeUnit.MILLISECONDS);
        } catch (CancellationException | TimeoutException unused) {
            return new j.a(b0.o, (List<j>) null);
        } catch (Exception unused2) {
            return new j.a(b0.j, (List<j>) null);
        }
    }

    public void h(m mVar, n nVar) {
        g gVar;
        if (!d()) {
            gVar = b0.n;
        } else {
            String a2 = mVar.a();
            List<String> b2 = mVar.b();
            String d2 = mVar.d();
            if (TextUtils.isEmpty(a2)) {
                c.a.a.a.a.k("BillingClient", "Please fix the input params. SKU type can't be empty.");
                gVar = b0.g;
            } else if (b2 == null) {
                c.a.a.a.a.k("BillingClient", "Please fix the input params. The list of SKUs can't be empty.");
                gVar = b0.f;
            } else if (!this.q && d2 != null) {
                c.a.a.a.a.k("BillingClient", "The user's client is too old to handle skuPackageName from SkuDetails.");
                gVar = b0.e;
            } else if (s(new j0(this, a2, b2, d2, nVar), 30000, new l0(this, nVar)) == null) {
                nVar.a(A(), (List<l>) null);
                return;
            } else {
                return;
            }
        }
        nVar.a(gVar, (List<l>) null);
    }

    public void i(e eVar) {
        ServiceInfo serviceInfo;
        String str;
        if (d()) {
            c.a.a.a.a.j("BillingClient", "Service connection is valid. No need to re-initialize.");
            eVar.b(b0.m);
            return;
        }
        int i2 = this.f2037a;
        if (i2 == 1) {
            c.a.a.a.a.k("BillingClient", "Client is already in the process of connecting to billing service.");
            eVar.b(b0.f2032d);
        } else if (i2 == 3) {
            c.a.a.a.a.k("BillingClient", "Client was already closed and can't be reused. Please create another instance.");
            eVar.b(b0.n);
        } else {
            this.f2037a = 1;
            this.f2040d.b();
            c.a.a.a.a.j("BillingClient", "Starting in-app billing setup.");
            this.i = new a(this, eVar, (k0) null);
            Intent intent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
            intent.setPackage("com.android.vending");
            List<ResolveInfo> queryIntentServices = this.e.getPackageManager().queryIntentServices(intent, 0);
            if (!(queryIntentServices == null || queryIntentServices.isEmpty() || (serviceInfo = queryIntentServices.get(0).serviceInfo) == null)) {
                String str2 = serviceInfo.packageName;
                String str3 = serviceInfo.name;
                if (!"com.android.vending".equals(str2) || str3 == null) {
                    str = "The device doesn't have valid Play Store.";
                } else {
                    ComponentName componentName = new ComponentName(str2, str3);
                    Intent intent2 = new Intent(intent);
                    intent2.setComponent(componentName);
                    intent2.putExtra("playBillingLibraryVersion", this.f2038b);
                    if (this.e.bindService(intent2, this.i, 1)) {
                        c.a.a.a.a.j("BillingClient", "Service was bonded successfully.");
                        return;
                    }
                    str = "Connection to Billing service is blocked.";
                }
                c.a.a.a.a.k("BillingClient", str);
            }
            this.f2037a = 0;
            c.a.a.a.a.j("BillingClient", "Billing service unavailable on device.");
            eVar.b(b0.f2031c);
        }
    }

    /* access modifiers changed from: package-private */
    public final l.a n(String str, List<String> list, String str2) {
        Bundle bundle;
        ArrayList arrayList = new ArrayList();
        int size = list.size();
        int i2 = 0;
        while (i2 < size) {
            int i3 = i2 + 20;
            ArrayList arrayList2 = new ArrayList(list.subList(i2, i3 > size ? size : i3));
            Bundle bundle2 = new Bundle();
            bundle2.putStringArrayList("ITEM_ID_LIST", arrayList2);
            bundle2.putString("playBillingLibraryVersion", this.f2038b);
            try {
                if (this.o) {
                    bundle = this.h.y(10, this.e.getPackageName(), str, bundle2, c.a.a.a.a.l(this.n, this.q, this.r, this.f2038b, str2));
                    String str3 = str;
                } else {
                    String str4 = str2;
                    bundle = this.h.I(3, this.e.getPackageName(), str, bundle2);
                }
                if (bundle == null) {
                    c.a.a.a.a.k("BillingClient", "querySkuDetailsAsync got null sku details list");
                    return new l.a(4, "Null sku details list", (List<l>) null);
                } else if (!bundle.containsKey("DETAILS_LIST")) {
                    int h2 = c.a.a.a.a.h(bundle, "BillingClient");
                    String g2 = c.a.a.a.a.g(bundle, "BillingClient");
                    if (h2 != 0) {
                        StringBuilder sb = new StringBuilder(50);
                        sb.append("getSkuDetails() failed. Response code: ");
                        sb.append(h2);
                        c.a.a.a.a.k("BillingClient", sb.toString());
                        return new l.a(h2, g2, arrayList);
                    }
                    c.a.a.a.a.k("BillingClient", "getSkuDetails() returned a bundle with neither an error nor a detail list.");
                    return new l.a(6, g2, arrayList);
                } else {
                    ArrayList<String> stringArrayList = bundle.getStringArrayList("DETAILS_LIST");
                    if (stringArrayList == null) {
                        c.a.a.a.a.k("BillingClient", "querySkuDetailsAsync got null response list");
                        return new l.a(4, "querySkuDetailsAsync got null response list", (List<l>) null);
                    }
                    int i4 = 0;
                    while (i4 < stringArrayList.size()) {
                        try {
                            l lVar = new l(stringArrayList.get(i4));
                            String valueOf = String.valueOf(lVar);
                            StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf).length() + 17);
                            sb2.append("Got sku details: ");
                            sb2.append(valueOf);
                            c.a.a.a.a.j("BillingClient", sb2.toString());
                            arrayList.add(lVar);
                            i4++;
                        } catch (JSONException unused) {
                            c.a.a.a.a.k("BillingClient", "Got a JSON exception trying to decode SkuDetails.");
                            return new l.a(6, "Error trying to decode SkuDetails.", (List<l>) null);
                        }
                    }
                    i2 = i3;
                }
            } catch (Exception e2) {
                String valueOf2 = String.valueOf(e2);
                StringBuilder sb3 = new StringBuilder("querySkuDetailsAsync got a remote exception (try to reconnect).".length() + String.valueOf(valueOf2).length());
                sb3.append("querySkuDetailsAsync got a remote exception (try to reconnect).");
                sb3.append(valueOf2);
                c.a.a.a.a.k("BillingClient", sb3.toString());
                return new l.a(-1, "Service connection is disconnected.", (List<l>) null);
            }
        }
        return new l.a(0, "", arrayList);
    }
}
